package com.controller.base;

import com.common.WebContextHelper;
import com.common.CollectionUtil;
import com.common.ImgCodeBuilderUtil;
import com.entity.Manager;
import com.facade.user.r.IManagerFacadeR;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 没有登录也能访问的Controller
 */
@Controller
@RequestMapping("/direct")
public class DirectAccessController {

    @Resource
    private IManagerFacadeR managerFacadeR;

    /**
     * 返回登录的Jsp页面
     *
     * @return
     */
    @RequestMapping(value = "/login.htm", method = RequestMethod.GET)
    public String goLoginView() {
        return "common/login";
    }

    /**
     * 用户登录请求
     */
    @RequestMapping(value = "/login.htm", method = RequestMethod.POST)
    public String checkUser(HttpServletRequest request, String name, String password,
                            /*@RequestParam(required = true) */String code, ModelMap modelMap) {
        /*if (!ImgCodeHelper.validVCode(request, code)) {
            modelMap.addAttribute("tip", "验证码错误...");
            modelMap.addAttribute("name", name);
            modelMap.addAttribute("password", password);
            return "common/login";
        }*/

        if (StringUtils.isBlank(StringUtils.trim(name))
                || StringUtils.isBlank(StringUtils.trim(password))) {
            modelMap.put("tip", "用户名和密码不能为空...");
            modelMap.put("name", name);
            modelMap.put("password", password);
            return "common/login";
        }

        name = StringUtils.trim(name);
        password = StringUtils.trim(password);
        String encPassword = DigestUtils.md5Hex(password);
        Map<String, Object> params = new HashMap<>();
        params.put("mobile", name);
        params.put("password", encPassword);
        Manager manager = managerFacadeR.findUniqueByPropertiesEqualTo(params);
        if (null == manager) {
            params.remove("mobile");
            params.put("name", name);
            List<Manager> userList = managerFacadeR.findByPropertiesEqualTo(params);
            if (!CollectionUtil.isEmpty(userList)) {
                if (userList.size() >= 2) {
                    modelMap.put("tip", "用户名称不能确认你的唯一身份，请使用手机号登录...");
                    modelMap.put("name", name);
                    modelMap.put("password", password);
                    return "common/login";
                } else {
                    manager = userList.get(0);
                }
            }
        }
        if (null == manager) {
            modelMap.addAttribute("tip", "登录名或密码错误...");
            modelMap.addAttribute("name", name);
            modelMap.addAttribute("password", password);
            return "common/login";
        }
        if (manager.isDisabled()) {
            modelMap.addAttribute("tip", "帐号被锁定，暂时不能登录系统...");
            modelMap.addAttribute("name", name);
            modelMap.addAttribute("password", password);
            return "common/login";
        }
        if (null == manager.getStatus()
                || Integer.valueOf("0").equals(manager.getStatus())) {
            modelMap.addAttribute("tip", "你的账号正在审核中，暂时不能登录系统...");
            modelMap.addAttribute("name", name);
            modelMap.addAttribute("password", password);
            return "common/login";
        } else if (Integer.valueOf("1").equals(manager.getStatus())) {
            modelMap.addAttribute("tip", "你的账号没有通过审核，不能登录系统，请联系管理员...");
            modelMap.addAttribute("name", name);
            modelMap.addAttribute("password", password);
            return "common/login";
        }

        WebContextHelper.invalidateSession();
        WebContextHelper.setOnLineUser(manager);
        return "redirect:/direct/index.htm";
    }

    /**
     * 登录后重定向到主页
     *
     * @return
     */
    @RequestMapping(value = "/index.htm")
    public String index() {
        Manager onLineUser = WebContextHelper.getOnLineUser();
        /*刷新主页session过时重定向到登录页面*/
        if (null == onLineUser) {
            return "redirect:/direct/login.htm";
        }
        return "common/index";
    }

    /**
     * 空白页
     */
    @RequestMapping(value = "/blank.htm")
    public String blank() {
        Manager onLineUser = WebContextHelper.getOnLineUser();
        /*刷新主页session过时重定向到登录页面*/
        if (null == onLineUser) {
            return "redirect:/direct/login.htm";
        }
        return "common/blank";
    }

    /**
     * 安全退出登录
     *
     * @return
     */
    @RequestMapping(value = "/logout.htm", method = RequestMethod.GET)
    public String logout() {
        WebContextHelper.removeOnLineUser();
        WebContextHelper.invalidateSession();
        return "common/login";
    }

    /**
     * 获取图片验证码请求方法
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/captcha.htm", method = RequestMethod.GET)
    public void generateCaptcha(HttpServletRequest request, HttpServletResponse response) {
        ImgCodeBuilderUtil.generateValidateCodeAndOutput(request, response);
    }

}